home *** CD-ROM | disk | FTP | other *** search
/ CICA 1993 April / CICA MS Windows - April 1993.iso / unzipped / programr / listings / blx12 / pets.cpp next >
Text File  |  1991-05-02  |  649b  |  34 lines

  1. // PETS.CPP : Conversations with your animals
  2.  
  3. #include <stdio.h>
  4.  
  5. struct pet {
  6.    virtual void speak() {}  // function definition
  7. };
  8.  
  9. struct dog : pet {
  10.    void speak() { puts("Bark!"); }
  11. };
  12.  
  13. struct cat : pet {
  14.    void speak() { puts("Meow!"); }
  15. };
  16.  
  17. struct bird : pet {
  18.    void speak() { puts("Tweet!"); }
  19. };
  20.  
  21. struct goldfish : pet {
  22.    void speak() { puts("!"); }
  23. };
  24.  
  25.                 // aggregate initialization of menagerie
  26. pet* menagerie[] = { new dog, new cat, new bird };
  27. const int sz = sizeof(menagerie)/sizeof(menagerie[0]);
  28.  
  29. void main(void)
  30. {
  31.    for (int i = 0; i < sz; i++)
  32.       menagerie[i]->speak();
  33. }
  34.